import { _decorator, Camera, Component, director, instantiate, Label, Node, Prefab, tween } from 'cc';
import { SpriteSwitch } from '../AshFramework/UI/SpriteSwitch';
import { ZTool } from '../AshFramework/Utils/ZTool';
import { WEAPONPROPERTY, WEAPONTYPE } from './GameConstant';
import { PrefsManager } from './PrefsManager';
import { ResourceUtil } from '../AshFramework/Utils/ResourceUtil';
import { GameManager } from './GameManager';
import { NumberIncrementLabel } from './NumberIncrementLabel';
import { BoxManager } from './BoxManager';
import Banner from '../Banner';
import { BlankScreen } from './BlankScreen';
import { Panel, UIManager } from '../AshFramework/Managers/UIManager';
import { AudioManager, Audios } from '../AshFramework/Managers/AudioManager';
import { BundleManager, Bundles } from '../AshFramework/Managers/BundleManager';
const { ccclass, property } = _decorator;

@ccclass('MenuManager')
export class MenuManager extends Component {
    public static Instance: MenuManager = null;

    @property(Node)
    SetPanel: Node = null;

    @property(Node)
    Music: Node = null;

    @property(Node)
    Shake: Node = null;

    @property(Node)
    BattlePanel: Node = null;

    @property(Node)
    ShopPanel: Node = null;

    @property(Node)
    WarehousePanel: Node = null;

    @property(Node)
    WeaponNode: Node = null;

    @property(Node)
    Canvas: Node = null;

    @property
    Price: number = 100;

    @property(Node)
    GoldBtn: Node = null;

    @property(Node)
    VideoBtn: Node = null;

    @property(NumberIncrementLabel)
    Gold: NumberIncrementLabel = null;

    @property(Camera)
    Camera: Camera = null;

    TargetPanel: Node = null;

    protected onLoad(): void {
        MenuManager.Instance = this;
        AudioManager.StopAll();
        AudioManager.PlayMusic(Audios.BGMusci);
        Banner.Instance.StartPopupAd(40);
    }

    start(): void {
        this.TargetPanel = this.BattlePanel;
        this.showWeapon();
        this.showNewPieceBtn();
        this.showMoney();
    }

    //展示剩余的钱
    showMoney(number: number = 0) {
        PrefsManager.Instance.userData.Gold += number;
        PrefsManager.Instance.saveData();
        this.Gold.playNumberIncrementTo(PrefsManager.Instance.userData.Gold);
    }


    setBtn() {
        AudioManager.PlaySound(Audios.Click);
        this.SetPanel.active = !this.SetPanel.active;
    }

    newPieceByGoldBtn() {
        AudioManager.PlaySound(Audios.Click);
        const box: Node = BoxManager.Instance.getBox();
        if (box) {
            PrefsManager.Instance.userData.Gold -= this.Price;
            this.showMoney();
            BoxManager.Instance.addNewPiece();
        } else {
        }
        this.showNewPieceBtn();
    }

    newPieceByVideoBtn() {
        AudioManager.PlaySound(Audios.Click);
        const box: Node = BoxManager.Instance.getBox();
        if (box) {
            BoxManager.Instance.addNewPiece();
            Banner.Instance.ShowVideoAd(() => { BoxManager.Instance.addNewPiece(); })
        } else {
        }
        this.showNewPieceBtn();
    }

    //展示获取零件的按钮
    showNewPieceBtn() {
        if (PrefsManager.Instance.userData.Gold >= this.Price) {
            this.GoldBtn.active = true;
            this.VideoBtn.active = false;
        } else {
            this.GoldBtn.active = false;
            this.VideoBtn.active = true;
        }
    }

    battleBtn() {
        AudioManager.PlaySound(Audios.Click);
        ResourceUtil.LoadPrefab("过渡黑屏").then((prefab: Prefab) => {
            const node: Node = instantiate(prefab);
            node.parent = this.Canvas;
            node.getComponent(BlankScreen).show(() => {
                director.loadScene("Game");
            })
        })
    }

    /**
     * 设置声音
     */
    musicBtn() {
        if (!AudioManager.Mute) {
            AudioManager.PlaySound(Audios.Click);
            AudioManager.StopAll();
        } else {
            AudioManager.PlayMusic(Audios.BGMusci);
        }
        AudioManager.Mute = !AudioManager.Mute;
        this.Music.getComponent(SpriteSwitch).switch();
    }

    /**
     * 设置震动
     */
    shakeBtn() {
        AudioManager.PlaySound(Audios.Click);
        this.Shake.getComponent(SpriteSwitch).switch();
    }

    warehousePanelBtn() {
        AudioManager.PlaySound(Audios.Click);
        if (this.TargetPanel && this.TargetPanel.active) this.TargetPanel.active = false;
        this.TargetPanel = this.WarehousePanel;
        this.TargetPanel.active = true;
    }


    shopPanelBtn() {
        AudioManager.PlaySound(Audios.Click);
        if (this.TargetPanel && this.TargetPanel.active) this.TargetPanel.active = false;
        this.TargetPanel = this.ShopPanel;
        this.TargetPanel.active = true;
    }

    backBtn() {
        AudioManager.PlaySound(Audios.Click);
        if (this.TargetPanel && this.TargetPanel.active) this.TargetPanel.active = false;
        this.TargetPanel = this.BattlePanel;
        this.TargetPanel.active = true;
        this.showWeapon();
    }

    showWeapon() {
        this.WeaponNode.removeAllChildren();
        const path: string = "Weapons/" + ZTool.GetEnumKeyByValue(WEAPONTYPE, PrefsManager.Instance.userData.CurWeapon);
        BundleManager.LoadPrefab(Bundles.Prefabs, path).then((prefab: Prefab) => {
            const weapon: Node = instantiate(prefab);
            weapon.parent = this.WeaponNode;
        })
    }

    privacyBtn() {
        AudioManager.PlaySound(Audios.Click);
        if (Banner.IS_ANDROID) {
            Banner.Instance.AndroidPrivacy();
        } else {
            UIManager.GetInstance().ShowPanel(Panel.PrivacyPanel, [false, () => { UIManager.GetInstance().HidePanel(Panel.PrivacyPanel) }]);
        }
    }


}


